<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Controllers;

use App\Admin\Repositories\PaymentAccount;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;

class PaymentAccountController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PaymentAccount(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('type', '账户')->using([
                1 => '银行账户',
                2 => '收款码',
            ]);
            $grid->column('is_enabled')->switch();
            $grid->column('bank_name');
            $grid->column('branch');
            $grid->column('account_name');
            $grid->column('account_number');
            $grid->column('qrcode_type', '收款码机构')->using([
                1 => '支付宝',
                2 => '微信',
            ]);
            $grid->column('qrcode_image')->image('', 50, 50);
            $grid->column('qrcode_url');
            $grid->column('remarks');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->between('created_at', '创建时间')->datetime();
                $filter->between('updated_at', '修改时间')->datetime();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new PaymentAccount(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('type');
            $show->field('is_enabled');
            $show->field('bank_name');
            $show->field('branch');
            $show->field('account_name');
            $show->field('account_number');
            $show->field('qrcode_type');
            $show->field('qrcode_image');
            $show->field('qrcode_url');
            $show->field('remarks');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new PaymentAccount(), function (Form $form) {
            $form->display('id');
            $form->text('name');
            $form->select('type')
                ->when(1, function (Form $form) {
                    $form->text('bank_name');
                    $form->text('branch');
                    $form->text('account_name');
                    $form->text('account_number');
                })
                ->when(2, function (Form $form) {
                    $form->select('qrcode_type')->options([
                        1 => '支付宝',
                        2 => '微信',
                    ]);
                    $form->image('qrcode_image')->autoUpload();
                    $form->url('qrcode_url');
                })->options([
                    1 => '银行账户',
                    2 => '收款码',
                ])->default(1);

            $form->text('remarks');
            $form->switch('is_enabled');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
